package com.example.healthcode2.Fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.healthcode2.Object.UserMsg;

import com.example.healthcode2.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;


public class threeFragment extends Fragment {
    private View view;
    private Button button;
    private AlertDialog dialog;
    private ExitOutListener exitOutListener;
    private TextView text_name2;
    private TextView text_number;
    private TextView text_phone;
    private TextView text_city;
    private UserMsg usermsg = null;
    private String name;
    private final static int JSON_OK = 2;
    private final static int JSON_ERROR = 3;


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        exitOutListener = (ExitOutListener) context;
    }

    public threeFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_three, container, false);
        initView();
        return view;
    }

    private void initView() {
        button = view.findViewById(R.id.three_btn_exit);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exitdialogshow();
            }
        });
        TextView text_name = view.findViewById(R.id.three_text_name);
        text_name2 = view.findViewById(R.id.three_text_name2);
        text_phone = view.findViewById(R.id.three_text_phone);
        text_number = view.findViewById(R.id.three_text_number);
        text_city = view.findViewById(R.id.three_text_city);

        name = exitOutListener.Getnameforthree();
        text_name.setText(name);
        httpRequest();

    }


    //退出账号对话框
    private void exitdialogshow() {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_exit, null);
        dialog = new AlertDialog.Builder(getContext(), R.style.MyDialog).setView(view).create();

        Button quxiao = view.findViewById(R.id.dialog_btn_exit_no);
        Button queding = view.findViewById(R.id.dialog_btn_exit_ok);

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    dialog.dismiss();
                    exitOutListener.deleteUser();//退出账号
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
    }

    //发送http请求
    private void httpRequest() {

        RequestQueue queue = Volley.newRequestQueue(getContext());
        String url = "http://30r97104b2.qicp.vip/user/find";
        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        Gson gson = new Gson();
                        Type type = new TypeToken<UserMsg>() {
                        }.getType();
                        usermsg = gson.fromJson(s, type);

                        myhandler.sendEmptyMessage(JSON_OK);
                        System.out.println(usermsg.toString());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        myhandler.sendEmptyMessage(JSON_ERROR);
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("username", name);

                return params;
            }
        };
        queue.add(request);


    }

    @SuppressLint("HandlerLeak")
    private Handler myhandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case JSON_OK:
                    text_name2.setText(usermsg.getName1());
                    text_phone.setText(usermsg.getPhone());
                    text_number.setText(usermsg.getId_card());
                    text_city.setText(usermsg.getAddress());
                    break;
                case JSON_ERROR:
                    Toast.makeText(getContext(),"网络异常",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


    //定义接口
    public interface ExitOutListener {
        void deleteUser() throws IOException;
        String Getnameforthree();
    }

}
